package com.fer.hr.nmbp.lab2.gui;

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.Reader;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import com.hr.fer.nmbp.lab2.persistence.GuestMapper;
import com.hr.fer.nmbp.lab2.test.NumberOfGuests;

public class MainWindow {
	public static SqlSessionFactory sessionFactory;
	private JFrame frmSustavZaRezervacije;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					MainWindow window = new MainWindow();
					window.frmSustavZaRezervacije.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 * @throws IOException 
	 */
	public MainWindow() throws IOException {
		initialize();
	}
	
	private void closeClick(){
		frmSustavZaRezervacije.dispose();
		System.exit(0);
	}

	private void guestNumClick(){
		SqlSession s = sessionFactory.openSession();
		GuestMapper mapper = s.getMapper(GuestMapper.class);
		NumberOfGuests nog = new NumberOfGuests();
		mapper.callGetTotalGuest(nog);
		Integer guestNumber = nog.getNumber();
		JOptionPane.showMessageDialog(frmSustavZaRezervacije, "Broj registriranih gostiju je: " + guestNumber.toString());
	}
	
	/**
	 * Initialize the contents of the frame.
	 * @throws IOException 
	 */
	private void initialize() throws IOException {
        String aResource = "myBatisConfig.xml";
        Reader reader = Resources.getResourceAsReader(aResource);
        sessionFactory = new SqlSessionFactoryBuilder().build(reader);
        
		frmSustavZaRezervacije = new JFrame();
		frmSustavZaRezervacije.setTitle("Sustav za rezervacije");
		frmSustavZaRezervacije.setBounds(100, 100, 220, 385);
		frmSustavZaRezervacije.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frmSustavZaRezervacije.getContentPane().setLayout(null);
		
		JButton btnDodajSobu = new JButton("Dodaj sobu");
		btnDodajSobu.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				AddRoomWindow addRoom = new AddRoomWindow();
				addRoom.setVisible(true);
			}
		});
		btnDodajSobu.setBounds(10, 32, 175, 23);
		frmSustavZaRezervacije.getContentPane().add(btnDodajSobu);
		
		JButton btnProvjeriRaspoloivost = new JButton("Provjeri raspoloživost");
		btnProvjeriRaspoloivost.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				CheckReservationsWindow checkReservations = new CheckReservationsWindow();
				checkReservations.setVisible(true);
			}
		});
		btnProvjeriRaspoloivost.setBounds(10, 135, 175, 23);
		frmSustavZaRezervacije.getContentPane().add(btnProvjeriRaspoloivost);
		
		JButton btnDodajRezervaciju = new JButton("Dodaj rezervaciju");
		btnDodajRezervaciju.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				AddReservationWindow addReservation = new AddReservationWindow();
				addReservation.setVisible(true);
			}
		});
		btnDodajRezervaciju.setBounds(10, 169, 175, 23);
		frmSustavZaRezervacije.getContentPane().add(btnDodajRezervaciju);
		
		JButton btnBrojGostiju = new JButton("Broj gostiju");
		btnBrojGostiju.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				guestNumClick();
			}
		});
		btnBrojGostiju.setBounds(10, 203, 175, 23);
		frmSustavZaRezervacije.getContentPane().add(btnBrojGostiju);
		
		JButton btnPretragaGostiju = new JButton("Pretraga gostiju");
		btnPretragaGostiju.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				SearchGuestsWindow searchGuests = new SearchGuestsWindow();
				searchGuests.setVisible(true);
			}
		});
		btnPretragaGostiju.setBounds(10, 235, 175, 23);
		frmSustavZaRezervacije.getContentPane().add(btnPretragaGostiju);
		
		JButton btnIzlaz = new JButton("Izlaz");
		btnIzlaz.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				closeClick();
			}
		});
		btnIzlaz.setBounds(10, 313, 175, 23);
		frmSustavZaRezervacije.getContentPane().add(btnIzlaz);
		
		JButton btnDeleteRoom = new JButton("Pobrisi sobu");
		btnDeleteRoom.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				DeleteRoomWindow deleteRoom = new DeleteRoomWindow();
				deleteRoom.setVisible(true);
			}
		});
		btnDeleteRoom.setBounds(10, 66, 175, 23);
		frmSustavZaRezervacije.getContentPane().add(btnDeleteRoom);
		
		JButton btnIzmjeniSobu = new JButton("Izmjeni sobu");
		btnIzmjeniSobu.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				ModifyRoomWindow modifyRoom = new ModifyRoomWindow();
				modifyRoom.setVisible(true);
			}
		});
		btnIzmjeniSobu.setBounds(10, 101, 175, 23);
		frmSustavZaRezervacije.getContentPane().add(btnIzmjeniSobu);
	}
}
